//var ufile = {fid:0,filepath:"",name:""};						//swfu
//var userfile = {fid:0,selectfileid:0,name:"",path:"",target:""};//fileMan
var fileMan;

$(function() {
	fileMan = (function() {//1.json:userfile 2.html:filelist 3.file_handler.js 4.util.js 5.site
		var debug	= false,
		modal 		= "#myFiles",				//bootcss modal name
		btn_select 	= "#btn_select",			//select btn
		btn_search 	= "#btn_search",			//search btn
		btn_choice  = ".btn_choice",			//choose filetype
		btn_open 	= ".btn_open",			//open dialog
		btn_close 	= ".btn_close",			//close dialog		
		input_search= "#searchkeys",			//search key
		text_message= ".sms",					//message element
		image_path  = site.img,					//website image path
		request_url	= site.web+"my/myfile/list",
		request_uid = site.uid,
		open_d_handler = file_open_handler,
		close_d_handler = file_close_handler,
		select_f_handler = file_select_handler,
		
		init			= function() {
			_initButton();			//设置按钮
		},
		
		_initButton = function(){
			$(btn_select).click(function(){//保存选择后的文件id在userfile中
				_callback(select_f_handler);
				$(modal).modal('hide');
			});	
			
			$(btn_search).click(function(){//根据值查询文件
				$(".typelist a").each(function(index,value){
					if($(this).hasClass("active")){
						getfile($(this).attr("key"),$(input_search).val());
					}
				});
			});
			
			$('.tabbable a').click(function (e) {//切换按钮
				 var view = $(this).attr("data-view");
				 switch(view){
				 case "upload":
					 break;
				 case "list":
					 getfile("photo");			
					 break;
				 }
				  e.preventDefault();
				  $(this).tab('show');
				})
			$(btn_choice).click(function(){//选择类型按钮
				getfile($(this).attr("key"));
			});
			
			$(btn_open).click(function(){//打开对话框
				$(modal).modal('show');
				_callback(open_d_handler,this);
			});
			$(btn_close).click(function(){//关闭对话框
				$(modal).modal('hide');
				_callback(close_d_handler);
			});
		},	
			
		/**
		 * 		type:photo,music,other
		 * searchkey:keyword
		 */
		getfile = function(type,searchkey){
			//[1]设置类别样式
			$(".file-"+type).addClass("active");
			//[2]获取改用户上传文件列表
			 cb = function(data){
			 	if(data.code!=1)return;//错误直接返回
		 		
		 		var files = data.data;
		 		$(".file_lists").empty();
		 		if(files.length!=0){			 			
		 			var ulist = "";					
					div = function(clas,keys){
						return "<div class='"+clas+"'>"+keys+"</div>";
					}
					caption = function(name){
						return "<p class='margin10tb'>"+name+"</p>";
					}
					img = function(src,name){//非图片类型直接生成名字按钮
						switch(type){
							case "photo":
								return "<img class='file_img_item' data-src='holder.js/120x120' src='"+src+"'/>";
								break;
							case "music":
							case "other":
								var html = "<img class='file_img_item' data-src='holder.js/120x120' src='"+image_path+"defaultfile.png'/>";
								html += div("caption",caption(substring(name,17)));
								return html;
								break;
						}
					}					
					
					filediv = function(src,fid,name,path){
						var value = "";
						value += "<li>";
						value += "<div class='thumbnail filediv' fid='"+fid+"' title='"+name+"' filepath='"+path+"'>";							
						value += img(src,name);
						value += "</div>"
						value += "</li>";
						return value;
					}
					
					//[3]显示文件	
		 			$.each(files,function(index,value){
		 				var src 	= site.web+files[index]['fileimg'],
		 					name 	= files[index]['filename'],
		 					path 	= files[index]['filepath'],
		 					fid 	= files[index]['fid'];
		 				ulist += filediv(src,fid,name,path);
		 			});
		 				
		 			$('.filediv').die(); //取消live绑定的方法
		 			
		 			//[4]点击文件的动作
		 			$(".filediv").live("click",function(){			 				
		 				var fid = $(this).attr("fid"),
		 				 	path =  $(this).attr("filepath"),
		 					name =$(this).attr("title");
		 				if(fid!=userfile.fid){//选择文件
		 					userfile.fid = fid;
		 					userfile.path = path;
		 					userfile.name = name;
		 					$(".filediv").removeClass("select_file");
		 					$(this).addClass("select_file");
		 					$(text_message).html("<span class='label label-info'>已选择文件:"+name+"</span>");
		 					$(btn_select).show();
		 				}else{//取消选择文件
		 					userfile.fid = 0;
		 					$(this).removeClass("select_file");
		 					$(text_message).html("");
		 					$(btn_select).hide();
		 				}			 				
					});
					$(".file_lists").append("<ul class='thumbnails'>"+ulist+"</ul>");
		 		}else{
					$(".file_lists").html("<span class='label'>请上传文件!<span>");
				}			 		
		 	
		 	 };
		 	 var json = {uid:request_uid,url:request_url,type:type,searchkey:searchkey};
		 	 $(".file_lists").html("<p>努力加载中...</p><img src='"+image_path+"ajax-loader.gif'/>");
		 	 G(json,cb);
		},
		
		_callback = function(func,arg){//回调函数
			if(arg){
				func(arg);
			}else{
				func();
			}
		};
		
		return { 
			init :init
		};
	})();
	fileMan.init();
}); 

